package cn.com.majiang.controller;

import cn.com.majiang.domain.Account;
import cn.com.majiang.domain.Manager;
import cn.com.majiang.enums.ResultCode;
import cn.com.majiang.param.AccountParam;
import cn.com.majiang.param.AddActualCardParam;
import cn.com.majiang.result.BaseResult;
import cn.com.majiang.service.IAccountService;
import cn.com.majiang.service.IManagerService;
import cn.com.majiang.service.ITechargerecordService;
import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * 账号管理
 * Created by jiangyanxin on 2017/5/10.
 */
@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/account")
public class AccountController {
    Logger logger = LoggerFactory.getLogger(AccountController.class);
    @Autowired
    IAccountService accountService;
    @Autowired
    ITechargerecordService techargerecordService;
    @Autowired
    IManagerService managerService;

    /**
     * 搜索玩家列表
     *
     * @throws Exception
     */
    @RequestMapping(value = "/searchAccountList", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public BaseResult searchAccountList(String str,HttpServletRequest request) throws Exception {
        logger.info("搜索玩家列表入参:{}", str);
        if (StringUtils.isBlank(str)) {
            return BaseResult.build(ResultCode.NULL_REQUESTPARAM);
        }
        AccountParam args = JSON.parseObject(str, AccountParam.class);
        Integer id = (Integer) request.getSession().getAttribute("id");
        if(id != 1){
            logger.info("表明不是管理员");
            if(args.getUuid() == null && StringUtils.isBlank(args.getName())){
                Map<String, Object> result = new HashMap();
                result.put("rows",new ArrayList<>());
                result.put("count",0);
                return BaseResult.build(result);
            }
            args.setStatus("0");
        }
        Map<String, Object> result = accountService.searchAccountList(args);
        return BaseResult.build(ResultCode.REQUEST_SUCCESS.getCode(), ResultCode.REQUEST_SUCCESS.getMessage(), result);
    }

    /**
     * 修改玩家状态(删除玩家)
     *
     * @param str
     */
    @RequestMapping(value = "/updateStatus", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public BaseResult updateAccountStatus(String str) {
        logger.info("更新玩家状态参数" + str);
        if (StringUtils.isBlank(str)) {
            return BaseResult.build(ResultCode.NULL_REQUESTPARAM);
        }
        Account args = JSON.parseObject(str, Account.class);
        if (accountService.editAccountStatus(args)) {
            return BaseResult.build(ResultCode.REQUEST_SUCCESS);
        } else {
            return BaseResult.build(ResultCode.REQUEST_FAIL);
        }
    }

    /**
     * 给玩家充值
     *
     * @param str
     */
    @RequestMapping("/addActualCard")
    @ResponseBody
    public BaseResult addActualCard(String str, HttpServletRequest request) {
        logger.info("给玩家充值" + str);
        if (StringUtils.isBlank(str)) {
            return BaseResult.build(ResultCode.NULL_REQUESTPARAM);
        }
        AddActualCardParam param = JSON.parseObject(str, AddActualCardParam.class);
        if (!StringUtils.isNumeric(param.getPlayCardNum())) {
            return BaseResult.build(ResultCode.REQUEST_PARAM_ERROR);
        }
        if(param.getId() == null || param.getUserid() == null ){
            return BaseResult.build(ResultCode.NULL_REQUESTPARAM);
        }
        int payCardNum = Integer.parseInt(param.getPlayCardNum());
        HttpSession session = request.getSession();
        Manager manager = (Manager) session.getAttribute("manager");
        if (manager != null) {
            if (accountService.updateRoomCard(param.getId(), manager, payCardNum)) {
                techargerecordService.saveAccountTechargerecord(param.getUserid(), manager.getName(), payCardNum,param.getChargetype());
                return BaseResult.build(ResultCode.REQUEST_SUCCESS);
            }
        }
        return BaseResult.build(ResultCode.REQUEST_FAIL);
    }

    /**
     * 将玩家升为代理
     *
     * @param id
     */
    @RequestMapping("/tomanager")
    @ResponseBody
    public BaseResult tomanager(String id, HttpServletRequest request) {
        logger.info("将玩家升为代理" + id);
        if (!StringUtils.isNumeric(id)) {
            return BaseResult.build(ResultCode.REQUEST_PARAM_ERROR);
        }
        HttpSession session = request.getSession();
        Integer managerId = (Integer) session.getAttribute("id");
        if (managerId == 1) {
            Account account = accountService.getAccountById(Integer.parseInt(id));
            if(account != null && "0".equals(account.getStatus()) && ("0".equals(account.getIsManager()) || account.getIsManager() == null)){
                logger.info("表明可以将升为代理");
                Manager manager = new Manager();
                manager.setManagerUpId(managerId);
                manager.setName(account.getUuid()+"");
                manager.setPowerId(2);
                manager.setPassword("123456");
                if(managerService.saveManager(manager)){
                    account.setIsManager("1");
                    accountService.updateIsManager(account);
                    return BaseResult.build(ResultCode.REQUEST_SUCCESS);
                }else{
                    return BaseResult.build(ResultCode.MANAGER_EXIST);
                }
            }
        }
        return BaseResult.build(ResultCode.REQUEST_FAIL);
    }


}
